package org.wltea.analyzer.lucene;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.synonym.SynonymFilterFactory;
import org.apache.lucene.analysis.util.ClasspathResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;

public class IkSynonym {

    private static SynonymFilterFactory synonymFilterFactory;

    public static SynonymFilterFactory getSynonymFilterFactory() {

        if (synonymFilterFactory == null) {
            synchronized (IkSynonym.class) {
                if (synonymFilterFactory == null) {
                    synonymFilterFactory = createSynonymFilterFactory();
                    return synonymFilterFactory;
                }
            }
        }
        return synonymFilterFactory;
    }

    private static SynonymFilterFactory createSynonymFilterFactory() {
        Map<String, String> paramsMap = new HashMap<>();
        Configuration cfg = DefaultConfig.getInstance();
        String synonyms = cfg.getExtSynonymDictionarys();
        if (synonyms == null) {
            synonyms = "synonyms.dic";
        }
        paramsMap.put("synonyms", synonyms);
        paramsMap.put("ignoreCase", Boolean.toString(true));
        SynonymFilterFactory factory = new SynonymFilterFactory(paramsMap);
        ResourceLoader loader = new ClasspathResourceLoader();
        System.out.println("synonyms path = " + cfg.getExtSynonymDictionarys());
        try {
            factory.inform(loader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return factory;
    }


}
